<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共166条记录</span>
        <template slot="after">
          <el-button
            size="small"
            type="warning"
            @click="$router.push('/import')"
          >导入</el-button>
          <el-button
            size="small"
            type="danger"
            @click="exportEmployee"
          >导出</el-button>
          <el-button
            size="small"
            type="primary"
            @click="showDialog = true"
          >新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格和分页 -->
      <el-card>
        <!-- 表格data 绑定列表数据，label代表每列标题名称，type= 'index'自动绑定序号，prop绑定数据里的具体值 -->
        <el-table border :data="list">
          <el-table-column label="序号" sortable="" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="图像" width="100">
            <template v-slot="{ row }">
              <img v-imgError="headImg" :src="row.staffPhoto" alt="" style="width:100% ; border-radius:50%" @click="showQRCode(row.staffPhoto)">
              <!-- <img v-imgError="require('@/assets/common/head.jpg')" :src="row.staffPhoto" alt="" style="width:100% ; border-radius:50%"> -->
            </template>
          </el-table-column>
          <el-table-column label="手机号" sortable="" prop="mobile" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <!-- formatter组件table-column提供的属性 ，格式化数据-->
          <el-table-column
            label="聘用形式"
            sortable=""
            prop="formOfEmployment"
            :formatter="formatEmployment"
          />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" sortable="" prop="enableState" /> -->
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template v-slot="{ row }">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="showAssignRole(row.id)">角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="delEmployee(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            layout="total,prev, pager, next"
            :current-page.sync="page.page"
            :page-size="page.size"
            :total="page.total"
            @current-change="getEmployeeList"
          >/>
          </el-pagination></el-row>
      </el-card>
    </div>
    <Employee :visible.sync="showDialog" />
    <AssignRole :visible.sync="showAssignDialog" :employee-id="employeeId" />
    <el-dialog
      title="二维码"
      :visible.sync="QRCodeVisible"
      width="30%"
    >
      <el-row type="flex" justify="center">
        <canvas ref="myCanvas" />
      </el-row>
    </el-dialog>

  </div>
</template>
<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeesEnum from '@/contant/employee'
import Employee from '@/views/employees/components/Employee.vue'
// 解决图片路径不识别问题
// ①在webpack中万物皆模块，图片也是一个模块，可以在组件中引入，放入data中就可以在模板中当变量使用了
// ②在template模板中使用require引入
import headImg from '@/assets/common/head.jpg'
import QRCode from 'qrcode'
import AssignRole from '@/views/employees/components/AssignRole.vue'
export default {
  components: { Employee, AssignRole },
  data() {
    return {
      headImg,
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      list: [],
      showDialog: false,
      QRCodeVisible: false,
      showAssignDialog: false,
      employeeId: ''
    }
  },
  created() {
    this.getEmployeeList()
  },

  methods: {
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList(this.page)
      this.page.total = total
      this.list = rows
    },
    formatEmployment(row, column, cellValue, index) {
      // console.log(row, column, cellValue, index)
      // if (cellValue === 1) return '正式'
      // if (cellValue === 0) return '非正式'
      // return '未知'
      // 注意，后台返回的是字符串，不用全等比较
      // eslint-disable-next-line eqeqeq
      const item = EmployeesEnum.hireType.find(item => item.id == cellValue)
      return item ? item.value : '未知'
    },
    async delEmployee(id) {
      try {
        await this.$confirm('确定删除吗？')
        await delEmployee(id)
        this.getEmployeeList()
        this.$message('删除成功！')
      } catch (error) {
        console.log(error)
      }
    },
    async exportEmployee() {
      const excel = await import('@/vendor/Export2Excel')
      const { rows } = await getEmployeeList({ page: 1, size: this.page.total })
      const { exportExcelMapPath, hireType } = EmployeesEnum
      // console.log(rows)

      // 表头数据
      const header = Object.keys(exportExcelMapPath)
      // const data = [['1380000', 'harry', '2022/12/10', '未知', '2022/12/15', '566', '中央']]
      // 数组里的对象转化为二维数组
      const data = rows.map(item => {
        // 遍历每一个对象，把对象转化为数组返回
        return header.map(ele => {
          const val = item[exportExcelMapPath[ele]]
          if (ele === '聘用形式') {
            // eslint-disable-next-line eqeqeq
            return hireType.find(t => t.id == val)?.value || '未知'
          }
          return val
        })
      })
      // console.log(data)
      excel.export_json_to_excel({
        header, // 表头
        data, // 表单的具体数据，是个二维数组
        filename: '员工信息表'
      })
    },
    showQRCode(url) {
      if (url) {
        this.QRCodeVisible = true
        // 因为dialog是重新创建的，dom是异步更新，所以需要在更新后拿到dom对象
        this.$nextTick(() => {
          QRCode.toCanvas(this.$refs.myCanvas, url)
        })
      }
    },
    showAssignRole(id) {
      this.employeeId = id
      this.showAssignDialog = true
    }

  }
}
</script>

<style scoped lang="less"></style>
